package com.kjs.web.handler;

import com.alibaba.fastjson.JSON;
import com.kjs.common.resultModel.JsonResult;
import com.kjs.execptionless.ExceptionCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * Created by zmk523@163.com on 2019/6/22 14:17
 */

@ControllerAdvice
public class ApiResultHandlerMiddleware implements ResponseBodyAdvice<Object> {

    /**
     * 判断哪些需要拦截
     */
    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) {

        //        return methodParameter.hasMethodAnnotation(ResponseBody.class);
        return true;
    }

    /*
     * 此处需注意在接口方法中返回String类型，则需用Map进行包装，否则有意想不到的问题
     * 加密解密：https://blog.csdn.net/yanmh007/article/details/88871705
     * body内容处理：https://blog.csdn.net/lrt890424/article/details/83627554
     * https://blog.csdn.net/qq_31289187/article/details/82980714
     **/
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        // 然后使用SpringBoot RequestContextHolder 提供的方法来获取Request 对象，来获取请求参数判断需要返回的对应类型

        System.out.println("TestResponseBodyAdvice==>beforeBodyWrite:" + body.toString() + "," + methodParameter + "," + request.getURI().getPath());
        System.out.println("请求返回数据类型class=" + body.getClass().getName());
        if (request.getURI().getPath().contains("swagger-resources") || request.getURI().getPath().contains("api-docs")) {
            return body;
        }

//        String enc = StringUtil.convertNULL(CallbackController.getRequestAttributes().getAttribute(ApiResultHandlerMiddleware.ENC, SCOPE_REQUEST));
//        boolean encBool = "1".equals(enc);
//        if (encBool) {
//            // 修改响应头
//            response.getHeaders().setContentType(MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE));
//        } else {
//            response.getHeaders().setContentType(MediaType.parseMediaType(MediaType.TEXT_XML_VALUE));
//            // 修改需要返回的字符串
//            body = AESUtil.encode(body);
//        }
//        if(methodParameter.hasMethodAnnotation())
//        {
//
//        }

        JsonResult result = null;
        //兼容原来的接口返回
        if (body instanceof JsonResult) {
            return body;
        }
//        else if (body instanceof PageResponseDataVo) {
//            result = (PageResponseDataVo) body;
//        } else if (body instanceof BaseResponseVo) {
//            result = (BaseResponseVo) body;
//        }

        //处理返回值是String的情况
        if (body instanceof String) {
            return new JsonResult(ExceptionCode.Normal.value(), "", body);
        }

        {
            return new JsonResult(ExceptionCode.Normal.value(), "", body);
        }
    }
}